/**
 * Created by chentao on 2015/8/21.
 */
var comment;
if (!comment)
    comment = {};

/**
 * 操作提示框显示
 * @param msg 提醒消息
 * @param callBack  提醒后的执行事件
 */
comment.showOperationTips = function (msg, callBack) {
    jqModal.showModal({
        content: '<label class="text-danger">' + msg + '</label>',
        type: 'CONFIRM',
        buttons: [{
            text: '确定',
            callback: function () {
                callBack();
            }
        }]
    });
}

comment.showOperationContent = function (msg, content, callBack) {
    jqModal.showModal({
        content: '<label class="text-danger">' + msg + '</label><input class="form-control" id="content" value="' + ((content == null) ? "" : content) + '"/>',
        type: 'CONFIRM',
        buttons: [{
            text: '确定',
            callback: function (tag) {
                var val = $(tag).find("#content").val();
                callBack(val);
            }
        }]
    });
}
/**
 * 刷新table首页面
 */
comment.refresh = function () {
    var table = $('#table');
    table.bootstrapTable('refresh');
}

//这个方法做了一些操作、然后调用回调函数
function doCallback(fn, args) {
    fn.apply(this, args);
}

comment.bindMultiSelect = function () {
    var selectDom = $("select[data-role=multiselect]");
    $.each(selectDom, function () {
        var url = $(this).attr("data-url");
        if (url != null) {
            var selectid = $(this).attr("id");
            var dvalue = $(this).attr("data-value");
            var dtext = $(this).attr("data-text");
            $.post($().AbsUrlParse(url), {}, function (res) {
                var data = res.res.data;
                if (data != null) {
                    var htm = "";
                    for (var i = 0; i < data.length; i++) {
                        var option = "<option value=" + getJsonProp(data[i], dvalue) + ">" + getJsonProp(data[i], dtext) + "</option> ";
                        htm = htm + option;
                    }
                    $("#" + selectid).html(htm);
                    $("#" + selectid).multiselect({enableFiltering: true});
                }
            });
        }
    });

}

var getJsonProp = function (obj, value) {
    for (var prop in obj) {
        if (prop == value) {
            return obj[prop];
        }
    }
}

comment.bindForm = function () {
    $(".form-submit").click(function () {
            var formBtn = $(this);
            var showMsg = $(this).data("showMsg");
            if (null != showMsg && true == showMsg) {
                comment.showOperationTips("您确定提交吗？", function () {
                    submitForm(formBtn);
                });
            } else {
                submitForm(formBtn);
            }
        }
    );
}

function submitForm(formBtn) {
    if (formBtn.attr("form-id") != null) {
        var form = $("#" + formBtn.attr("form-id"));
        form.validate();
        var v = form.valid();
        if (!v) {
            return false;
        }
        var params = form.formToArray();
        var url = form.attr("action");
        comment.post(url, params, '处理中', function (data) {
            form[0].reset();

            if (null != form.attr("data-callback")) {
                var callback = eval(form.attr("data-callback"));
                doCallback(callback, [JSON.stringify(data)]);
            }

            closeModel(form.parent().parent().parent().attr("id"));
            comment.refresh();
        });
    }
}

/**
 * post提交事件
 * @param url   地址
 * @param params    参数
 * @param msg   提示信息
 * @param success   成功回调
 */
comment.post = function (url, params, msg, success) {
    if (msg == null)
        msg = '加载中...';
    if (!url.startsWith("http://")) {
        url = $().AbsUrlParse(url);
    }
    $.blockUI(
        {
            message: msg,
            baseZ: 2000,
            css: {
                border: 'none',
                padding: '15px',
                backgroundColor: 'white',
                width: "300px",
                opacity: .5,
                color: 'black'
            }
        });
    $.post(url, params,
        function (data) {
            $.unblockUI();
            if (success != null) {
                if (data.state) {
                    if (data.res.code == 40000) {
                        // hint("处理成功");
                        success(data);
                    } else {
                        hint(data.res.msg, 5000, "danger");
                    }
                } else {
                    hint(data.msg, 5000, "danger");
                }
            }
        });
}


comment.get = function (url, msg, success) {
    if (msg == null)
        msg = '加载中...';
    if (!url.startsWith("http://")) {
        url = $().AbsUrlParse(url);
    }
    $.blockUI(
        {
            message: msg,
            baseZ: 2000,
            css: {
                border: 'none',
                padding: '15px',
                backgroundColor: 'white',
                width: "300px",
                opacity: .5,
                color: 'black'
            }
        });
    $.get(url,
        function (data) {
            $.unblockUI();
            if (success != null) {
                if (data.res.code == 40000)
                    success(data);
                else
                    hint(data.res.msg);
            } else {
                hint(data.msg);
            }
        });
}


/**
 * 判断输入是否为数字
 * @param v
 * @returns {boolean}
 */
function isNumber(v) {
    var str = v.val();
    var reg = parseFloat(str);
    var re = /^\+?[0-9][0-9]*$/;
    if (!re.test(str)) {
        return false;
    }
    return !isNaN(reg);
}